﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

public partial class Admin_AddProduct : System.Web.UI.Page
{
    ShopDataContext dc = new ShopDataContext();
    static Product p = new Product();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            var query = from c in dc.Categories
                        where c.cActive == true
                        select c;

            foreach (var item in query)
            {
                ddlCategory.Items.Add(new ListItem(item.cName, item.cId.ToString()));
            }
        }
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {

        p.pProductCode = txtProductCode.Text;
        p.pCategoryId = int.Parse(ddlCategory.SelectedValue);
        p.pName = txtName.Text;
        if(fuSmall.PostedFile != null)
        {
            string s = fuSmall.PostedFile.FileName;
        }
        p.pDescription = txtDescription.Text;
        p.pPrice = Convert.ToInt32(txtPrice.Text);
        p.pActive = chkActive.Checked;
        p.pQuantity = Convert.ToInt32(txtQuantity.Text);
        dc.Products.InsertOnSubmit(p);
        dc.SubmitChanges();
        txtName.Text = "";
        txtDescription.Text = "";
        txtPrice.Text = "";
        chkActive.Checked = false;
        p = new Product();

        Response.Redirect("ListProduct.aspx");
    }



    protected void ddlCategory_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlCategory.SelectedIndex != 0)
        {
            IdentityCode();
        }
    }

    private void IdentityCode()
    {

        int cCode = Convert.ToInt32(ddlCategory.SelectedValue);
        Product query = (from p in dc.Products
                         orderby p.pId descending
                         where p.pCategoryId == cCode && p.pActive == true
                         select p).FirstOrDefault();
        if (query != null)
        {
            string str = query.pProductCode;
            str = str.Substring(2);
            str = (Convert.ToInt32(str) + 1).ToString();
            switch (str.Length)
            {
                case 1: str = "0000" + str;
                    break;
                case 2: str = "000" + str;
                    break;
                case 3: str = "00" + str;
                    break;
                case 4: str = "0" + str;
                    break;
            }
            string cOd = cCode.ToString();
            if (cOd.Length == 1)
            {
                cOd = "0" + cOd;
            }
            txtProductCode.Text = cOd + str;
        }
        else
        {
            string cOd = cCode.ToString();
            if (cOd.Length == 1)
            {
                cOd = "0" + cOd;
            }
            txtProductCode.Text = cOd + "00001";
        }
    }
    protected void ddlCategory_Load(object sender, EventArgs e)
    {

    }
    protected void fuSmall_UploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
    {
        if (fuSmall.HasFile)
        {
            if(Path.GetExtension(e.FileName).ToLower().Contains(".gif")
                || Path.GetExtension(e.FileName).ToLower().Contains(".png")
                || Path.GetExtension(e.FileName).ToLower().Contains(".jpg"))
            {
                fuSmall.SaveAs(Server.MapPath("~/ImageProduct/") + "Small" + txtProductCode.Text + fuSmall.FileName);
                p.pSmallImage = "Small" + txtProductCode.Text + fuSmall.FileName;
            }
            else
            {
                return;
            }
        }
    }
    protected void fuBig_UploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
    {
        if (fuBig.HasFile)
        {
            if (Path.GetExtension(e.FileName).ToLower().Contains(".gif")
                || Path.GetExtension(e.FileName).ToLower().Contains(".png")
                || Path.GetExtension(e.FileName).ToLower().Contains(".jpg"))
            {
                fuBig.SaveAs(Server.MapPath("~/ImageProduct/") + "Big" + txtProductCode.Text + fuBig.FileName);
                p.pBigImage = "Big" + txtProductCode.Text + fuBig.FileName;
            }
            else
            {
                return;
            }
        }
    }
}